MowerFileInputReaderTestCase = TestCase("MowerFileInputReaderTestCase");

MowerFileInputReaderTestCase.prototype.testGetLawnCorner = function() {
	var lines = [
	             "5 6",
	             "1 2 N",
	             "ADG"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	var position = mowerFileInputReader.getLawnCorner();
	assertEquals(5, position.x);
	assertEquals(6, position.y);
};

MowerFileInputReaderTestCase.prototype.testGetLawnCornerMissing = function() {
	var lines = [
	             ""];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	assertException(MissingLawnCornerException, mowerFileInputReader.getLawnCorner);
};

MowerFileInputReaderTestCase.prototype.testGetLawnCornerUnparsable = function() {
	var lines = [
	             "1 abc"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	assertException(UnparsableLawnCornerException, mowerFileInputReader.getLawnCorner);
};

MowerFileInputReaderTestCase.prototype.testNextStartVector = function() {
	var lines = [
				"5 5",
				"3 4 N",
				"",
				"5 6 S",
				""];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	mowerFileInputReader.getLawnCorner();
	
	var vector = mowerFileInputReader.nextStartVector();
	assertEquals(3, vector.position.x);
	assertEquals(4, vector.position.y);
	assertEquals(DIRECTION.N, vector.direction);
	
	vector = mowerFileInputReader.nextStartVector();
	assertEquals(5, vector.position.x);
	assertEquals(6, vector.position.y);
	assertEquals(DIRECTION.S, vector.direction);
};

MowerFileInputReaderTestCase.prototype.testNextStartVectorUnparsable = function() {
	var lines = [
				"5 5",
				"3 4 N",
				"",
				"5 bla S",
				""];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	mowerFileInputReader.getLawnCorner();
	mowerFileInputReader.nextStartVector();
	assertException(UnparsableMowerStartVectorException, mowerFileInputReader.nextStartVector);
};

MowerFileInputReaderTestCase.prototype.testNextStartVectorSkipUnparsable = function() {
	var lines = [
				"5 5",
				"3 bla N",
				"",
				"5 6 S",
				""];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	mowerFileInputReader.getLawnCorner();
	
	//TODO: jsTestDriver bug?
	//assertException(UnparsableMowerStartVectorException, mowerFileInputReader.nextStartVector);
	var exp = null;
	try {
		mowerFileInputReader.nextStartVector();
	} catch(e) {
		exp = e;
	}
	assertTrue(UnparsableMowerStartVectorException.prototype.isPrototypeOf(exp));
	
	var mowerStartVector = mowerFileInputReader.nextStartVector();
	assertEquals(new MowerVector(new Position(5, 6), DIRECTION.S), mowerStartVector);
};

MowerFileInputReaderTestCase.prototype.testNextCommand = function() {
	var lines = [
				"5 5",
				"0 0 N",
				"DGA"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	var command = null;
	
	mowerFileInputReader.getLawnCorner();
	command = mowerFileInputReader.nextCommand();
	assertEquals(COMMAND.D, command);
	command = mowerFileInputReader.nextCommand();
	assertEquals(COMMAND.G, command);
	command = mowerFileInputReader.nextCommand();
	assertEquals(COMMAND.A, command);
};

MowerFileInputReaderTestCase.prototype.testnextCommandUnparsable = function() {
	var lines = [
				"5 5",
				"0 0 N",
				"DblaA"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	mowerFileInputReader.getLawnCorner();
	mowerFileInputReader.nextCommand();
	assertException(UnparsableCommandException, mowerFileInputReader.nextCommand);
};

MowerFileInputReaderTestCase.prototype.testNextCommandUnparsableSkip = function() {
	var lines = [
				"5 5",
				"0 0 N",
				"D?A"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	mowerFileInputReader.getLawnCorner();
	
	var command = null;
	
	command = mowerFileInputReader.nextCommand();
	assertEquals(COMMAND.D, command);
	
	//TODO: jsTestDriver bug?
	//assertException(UnparsableCommandException, mowerFileInputReader.nextCommand);
	var exp = null;
	try {
		mowerFileInputReader.nextCommand();
	} catch(e) {
		exp = e;
	}
	assertTrue(UnparsableCommandException.prototype.isPrototypeOf(exp));
	
	command = mowerFileInputReader.nextCommand();
	assertEquals(COMMAND.A, command);	
};

MowerFileInputReaderTestCase.prototype.testCallbacks = function() {
	var lines = [
				"5 5",
				"1 2 N",
				"GAG",
				"3 3 E",
				"AA"];
	var mowerFileInputReader = new MowerFileInputReader(null);
	mowerFileInputReader.lines = lines;
	
	var isMowerCommandsEnd = false;
	var isMowersEnded = false;
	mowerFileInputReader.setCurrentMowerCommandsEndCallback(function(){
		isMowerCommandsEnd = true;
	});
	mowerFileInputReader.setMowersEndedCallback(function(){
		isMowersEnded = true;
	});
	
	mowerFileInputReader.getLawnCorner();
	
	mowerFileInputReader.nextStartVector();
	mowerFileInputReader.nextCommand();
	mowerFileInputReader.nextCommand();
	mowerFileInputReader.nextCommand();
	assertFalse(isMowerCommandsEnd);
	mowerFileInputReader.nextCommand();
	assertTrue(isMowerCommandsEnd);
	isMowerCommandsEnd = false;
	
	mowerFileInputReader.nextStartVector();
	assertFalse(isMowersEnded);
	assertFalse(isMowerCommandsEnd);
	mowerFileInputReader.nextCommand();
	mowerFileInputReader.nextCommand();
	mowerFileInputReader.nextCommand();
	assertTrue(isMowerCommandsEnd);	
	
	mowerFileInputReader.nextStartVector();
	assertTrue(isMowersEnded);
};